package com.itheima.service.impl;

import com.itheima.Exception.BizException;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    /**
     *  查询所有部门
     * @return
     */
    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    /**
     *  根据id删除部门
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        Integer exists = empMapper.existsWithClazzId(id);
        if(exists > 0){
            throw new BizException("该部门下有员工，无法删除");
        }
        deptMapper.deleteById(id);
    }

    /**
     *  新增部门
     * @param dept
     */
    @Override
    public void save(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    /**
     *  根据id查询部门
     * @param id
     * @return
     */
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    /**
     *  修改部门
     * @param dept
     */
    public void update(Dept dept) {
        //补全基础属性
        dept.setUpdateTime(LocalDateTime.now());
        //保存部门
        deptMapper.update(dept);
    }
}
